def broadcast_env_change():
  import win32gui, win32con
  rc, dwReturnValue = win32gui.SendMessageTimeout(win32con.HWND_BROADCAST,
    win32con.WM_SETTINGCHANGE, 0, "Environment", win32con.SMTO_ABORTIFHUNG,5000)

if __name__=="__main__":
  import _winreg as reg,sys,os
  SYSTEM_ENV_SUBKEY  = "System\\CurrentControlSet\\Control\\Session Manager\\Environment";
  USER_ENV_SUBKEY = "Environment"

  key=reg.OpenKey(reg.HKEY_LOCAL_MACHINE,SYSTEM_ENV_SUBKEY, 0, reg.KEY_READ|reg.KEY_SET_VALUE)
  path,type=reg.QueryValueEx(key, "Path");
  path = path.split(';')
  dir = os.path.dirname(sys.argv[0])
  if dir:
    path = tuple(filter(lambda s:not 'win-pkg' in s,path)) + (dir,)
    reg.SetValueEx(key, "Path", "", type, ';'.join(path))

  broadcast_env_change()
  sys.exit(0)
